var main = {
    ls : window.localStorage ,
    search : urlParse() ,
    isVisitor : false ,
    curPhoto : null ,
    user : null ,
    shop : null ,
    photos : [] ,
    pid : 0 ,
    init : function () {
        var self = this;
        self.user = JSON.parse(self.ls.getItem('user'));
        self.shop = JSON.parse(self.ls.getItem('shop'));
        document.title = self.shop.name + '欢迎您,' + self.user.nickName;
        self.isVisitor = !!self.search.openId;
        self.getPublicImages();
        self.events();
        if (!self.isVisitor) {
            this.getPrivateImages();
        }
    } ,
    events : function () {
        var self = this;
        // 查看大图
        $('.photos').tap(function (e) {
            var target = e.target.parentNode;
            var src = e.target.attr('src').replace('_cut' , "");
            var curState = target.attr('state');
            self.pid = target.attr('pid');
            self.curPhoto = target;
            if (!self.isVisitor) {
                if (curState == "1") {
                    $('.turn-public').addClass('none');
                    $('.turn-private').removeClass('none');
                } else if (curState == '2') {
                    $('.turn-public').removeClass('none');
                    $('.turn-private').addClass('none');
                }
            } else {
                $('.delete').addClass('none');
            }
            $('.bigPhoto').removeClass('none');
            $('.bigPhoto img').attr('src' , src);
        });
        // 关闭大图
        $('.bigPhoto').touchstart(function () {
            this.addClass('none');
        });
        // 转为私密
        $('.turn-private').touchstart(function (e) {
            self.changeState(2 , self.pid);
        });
        // 转为公开
        $('.turn-public').touchstart(function (e) {
            self.changeState(1 , self.pid);
        });
        // 删除照片
        $('.delete').touchstart(function () {
            self.deletePhoto(self.pid);
        });
    } ,
    getPublicImages : function () {
        var self = this;
        ajax.get({
            url : '/snapshot/user/selectUserImages.do' ,
            data : {
                state : 1 ,
                no : 1 ,
                size : 1000 ,
                openId : self.isVisitor ? self.search.openId : null
            } ,
            success : function (resp) {
                self.getPublicImagesDone = true;
                if (resp.success && resp.value) {
                    self.photos = self.photos.concat(resp.value.userImages);
                    self.displayImages(self.photos);
                    console.log(self.photos);
                }
            }
        });
    } ,
    getPrivateImages : function () {
        var self = this;
        ajax.get({
            url : '/snapshot/user/selectUserImages.do' ,
            data : {
                state : 2 ,
                no : 1 ,
                size : 1000 ,
                openId : self.isVisitor ? self.search.openId : null
            } ,
            success : function (resp) {
                self.getPrivateImagesDone = true;
                if (resp.success && resp.value) {
                    self.photos = self.photos.concat(resp.value.userImages);
                    self.displayImages(self.photos);
                    console.log(self.photos);
                }
            }
        });
    } ,
    displayImages : function (photos) {
        var self = this;
        if (self.getPublicImagesDone && self.getPrivateImagesDone && !photos.length) {
            $('.photos').addClass('none');
            // 如果不是游客模式，则提供一个拍照的快捷入口
            if (!this.isVisitor) {
                $('.noPhotoRemind').removeClass('none');
                $('.toTakePhoto').href = "takePhotos.html";
            } else {
                $('.noPhotoRemind').removeClass('none');
                $('.noPhotoRemind p').innerText = "好遗憾，他还没有拍任何照片~";
                $('.toTakePhoto img').attr('src' , '../images/back.png');
                $('.toTakePhoto').href = "index.html";
            }
            return;
        }
        if (photos.length) {
            var width = "width:" + ($('.photos').offsetWidth - 15) / 3 + "px";
            $('.photos').html(template.render('photo-template' , {
                data : {
                    photos : photos ,
                    width : width ,
                    shopname : self.shop.name
                }
            }));
            self.arrangePhotos();
        }
    } ,
    changeState : function (state , imageId) {
        var self = this;
        ajax.get({
            url : '/snapshot/user/updateUserImages.do' ,
            data : {
                imageId : imageId ,
                state : state
            } ,
            success : function (resp) {
                if (resp.success) {
                    self.curPhoto.setAttribute('state' , state);
                    if (state == 1) {
                        self.curPhoto.find('.photo-type').innerText = "公开";
                    } else {
                        self.curPhoto.find('.photo-type').innerText = "私密";
                    }
                }
            }
        });
    } ,
    deletePhoto : function (imageIds) {
        var self = this;
        ajax.get(
            {
                url : '/snapshot/user/deleteImages.do' ,
                data : {
                    imageIds : imageIds
                } ,
                success : function (resp) {
                    if (resp.success) {
                        $('.photos').removeChild(self.curPhoto);
                        self.arrangePhotos();
                    }
                }
            }
        );
    } ,
    arrangePhotos : function () {
        var photos = $$(".photos .photo");
        for (var i = 0 ; i < photos.length ; i++) {
            if (i % 3 == 0) {
                photos[i].removeClass('ml5');
            } else if (i % 3 == 1) {
                photos[i].addClass('ml5');
            } else if (i % 3 == 2) {
                photos[i].addClass('ml5');
            }
        }
    }
};
window.addEventListener("DOMContentLoaded" , function () {
    main.init();
} , false);